package com.sunlee.common.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sunlee.common.core.constant.Constant;
import com.sunlee.common.core.utils.ConvertUtils;
import com.sunlee.common.core.utils.TreeUtils;
import com.sunlee.common.service.dao.ThirdCategoryMappingDao;
import com.sunlee.common.service.dto.CategoryDTO;
import com.sunlee.common.service.dto.ThirdCategoryMappingDTO;
import com.sunlee.common.service.entity.CategoryEntity;
import com.sunlee.common.service.entity.ThirdCategoryMappingEntity;
import com.sunlee.common.service.service.CategoryService;
import com.sunlee.common.service.service.ThirdCategoryMappingService;
import com.sunlee.common.service.service.ThirdCategoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 三方分类映射表
 *
 * @author sunlee
 * @date 2023-03-09
 */
@Service
public class ThirdCategoryMappingServiceImpl extends CrudServiceImpl<ThirdCategoryMappingDao, ThirdCategoryMappingEntity, ThirdCategoryMappingDTO> implements ThirdCategoryMappingService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ThirdCategoryService thirdCategoryService;


    /**
     * 通过第三方分类Id，查询己方分类ID
     */
    @Override
    public List<CategoryDTO> queryCategoryByThirdCategoryId(long thirdCategoryId) {
        // 查询己方全部分类
        List<CategoryEntity> allCategoryList = categoryService.list();
        // 转树
        List<CategoryDTO> allDtoList = TreeUtils.build(ConvertUtils.sourceToTarget(allCategoryList, CategoryDTO.class), Constant.MENU_ROOT);
        // 查询关联分类
        List<ThirdCategoryMappingEntity> mappingCategoryList = list(new LambdaQueryWrapper<ThirdCategoryMappingEntity>().eq(ThirdCategoryMappingEntity::getThirdCategoryId, thirdCategoryId));

        // 处理关联关系
        if (CollectionUtils.isNotEmpty(mappingCategoryList)) {
            Map<Long, List<ThirdCategoryMappingEntity>> mappingCategoryMap = mappingCategoryList.stream().collect(Collectors.groupingBy(ThirdCategoryMappingEntity::getCategoryId));
            // 遍历子分类，子分类存在一个符合，则父分类也当做符合
            allDtoList.forEach(parentCategory -> {
                parentCategory.getChildren().forEach(childrenCategory -> {
                    if (mappingCategoryMap.containsKey(childrenCategory.getId())) {
                        childrenCategory.setChoose(true);

                        if (Objects.isNull(parentCategory.getChoose())) {
                            parentCategory.setChoose(true);
                        }
                    }
                });

            });
        }

        return allDtoList;
    }

    @Override
    public boolean saveByMappingId(Long categoryId, Long thirdCategoryId) {
        ThirdCategoryMappingEntity thirdCategoryMappingEntity = new ThirdCategoryMappingEntity();
        thirdCategoryMappingEntity.setCategoryId(categoryId);
        thirdCategoryMappingEntity.setThirdCategoryId(thirdCategoryId);
        return save(thirdCategoryMappingEntity);
    }

    @Override
    public boolean removeByMappingId(Long categoryId, Long thirdCategoryId) {
        return remove(new LambdaQueryWrapper<ThirdCategoryMappingEntity>()
                .eq(ThirdCategoryMappingEntity::getCategoryId, categoryId)
                .eq(ThirdCategoryMappingEntity::getThirdCategoryId, thirdCategoryId));
    }
}
